home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 474 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.5 KB  |  30 lines

  1. Newsgroups: comp.std.c
  2. Path: howland.reston.ans.net!torn!sq!utzoo!spenford!henry
  3. From: Henry Spencer <henry@zoo.toronto.edu>
  4. Subject: Re: integral types in switch expressions
  5. References: <4eb5r1$b04@news.tuwien.ac.at>
  6. Sender: henry%spenford@zoo.toronto.edu (Henry Spencer)
  7. Organization: SP Systems, Toronto
  8. Date: Fri, 1 Mar 1996 04:47:54 GMT
  9. Message-ID: <DnKnzv.8t6%spenford@zoo.toronto.edu>
  10.  
  11. In article <4eb5r1$b04@news.tuwien.ac.at> schwarz@mips.complang.tuwien.ac.at (Konrad Schwarz) writes:
  12. >I was surprised to learn that constant pointer expressions are
  13. >not allowed in case labels and that the expression in a switch statement
  14. >must be an integral type.  (The reason for the exclusion of floating point
  15. >is obvious to me).  I am interested in the technical reasons for this.
  16.  
  17. Well, the practical reason is that C compilers have traditionally
  18. disallowed it and nobody made a convincing case for changing this. 
  19.  
  20. The technical reason for disallowing it is that doing even a vaguely
  21. clever implementation of switch requires knowing the values of the case
  22. labels at compile time.  The values of pointer expressions are typically
  23. not known until link time; on systems using dynamic linking, they may not
  24. be known until run time.  When the values are not known at compile time,
  25. it's hard to implement the switch as anything other than a series of
  26. ifs... and there is no point in writing that as a switch.
  27. -- 
  28. Space will not be opened by always                 |       Henry Spencer
  29. leaving it to another generation.   --Bill Gaubatz |   henry@zoo.toronto.edu
  30.